﻿using System;
using System.Threading;
using System.Threading.Tasks;
using log4net;
using RabbitMQ.Client.Events;

namespace Keede.RabbitMQ.QuickStart
{
    class Program
    {
        static int j = 0;

        static ILog _log = LogManager.GetLogger(typeof(Program));

        static void Main(string[] args)
        {
            _log.Info("Quick start...");

            var exchange1 = "test.exchange";
            var exchange2 = "test.exchange2";
            string queue1 = "OrderCreated", queue2 = "OrderCreated2";
            string routingKey = "OrderCreated";

            var auth1 = new Authorization("test.user", "123456", "test.virtual.host", "192.168.117.21", 5673);
            var auth2 = new Authorization("test.user", "123456", "test2.virtual.host", "192.168.117.21", 5673);
            var c1 = new Client(auth1, false,20);
            //var c2 = new Client(auth2, false);



            c1.ExchangeDeclare(exchange1, ExchangeType.Direct);
            c1.QueueDeclare(queue1);
            c1.QueueBind(exchange1, queue1, routingKey);

            c1.ExchangeDeclare(exchange2, ExchangeType.Direct);
            c1.QueueDeclare(queue2);
            c1.QueueBind(exchange2, queue2, routingKey);

            Console.WriteLine("Enter press P to switch publisher,and press C to switch consumer");
            var input = Console.ReadLine();
            if (input.ToLower() == "p")
            {
                int i = 0;
                while (true)
                {
                    try
                    {
                        if (i % 2 == 0) c1.Publish($"{i++}this is a test.virtual.host message", exchange1, routingKey);
                        else c1.Publish($"{i++}this is a test2.virtual.host message", exchange2, routingKey);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(i + e.Message);
                    }
                }
            }
            else if (input.ToLower() == "c")
            {
                c1.Subscribe(new QueueListener
                {
                    QueueName = queue1,
                    MessageReceivedHandler = MessageReceivedHandle
                });

                c1.Subscribe(new QueueListener
                {
                    QueueName = queue2,
                    MessageReceivedHandler = MessageReceivedHandle
                });
            }
            else
            {
                Console.WriteLine("Wrong command");
            }

            Console.ReadLine();
        }

        static Random r = new Random();
        /// <summary>
        /// with Ask handle
        /// </summary>
        static Task<bool> MessageReceivedHandle(string message, BasicDeliverEventArgs rawData)
        {
            return Task.Run(() =>
            {
                //handle
                Thread.Sleep(r.Next(2000,10000));
                Console.WriteLine($"{rawData.RoutingKey}|{message}");
                return false;//重新入队返回ture，否则返回false
            });
        }

        /// <summary>
        /// without Ask handle
        /// </summary>
        static Task MessageReceivedAutoAskHandler(string message, BasicDeliverEventArgs rawData)
        {
            return Task.Run(() =>
            {
                //handle
                Console.WriteLine($"{rawData.RoutingKey}|{message}");
            });
        }
    }
}
